package com.nineclock.sign.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.entity.Result;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.threadlocal.CurrentUserHolder;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.sign.dto.SignAllUserDTO;
import com.nineclock.sign.dto.SignDTO;
import com.nineclock.sign.mapper.SignPictureMapper;
import com.nineclock.sign.mapper.SignPunchMapper;
import com.nineclock.sign.pojo.SignInfo;
import com.nineclock.sign.pojo.SignPicture;
import com.nineclock.sign.service.SignPunchService;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.feign.SysUserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional
public class SignPunchServiceImpl implements SignPunchService {
    @Autowired
    private SignPunchMapper signPunchMapper;
    @Autowired
    private SignPictureMapper signPictureMapper;
    @Autowired
    private SysUserFeign sysUserFeign;



    @Override
    public void punch(SignDTO signDTO) {
        //参数校验
        if (signDTO == null) {
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        //1. 封装签到信息
        Date date = new Date();
        SignInfo signInfoList = BeanHelper.copyProperties(signDTO, SignInfo.class);
        signInfoList.setCompanyId(CurrentUserHolder.get().getCompanyId());
        signInfoList.setCompanyUserId(CurrentUserHolder.get().getCompanyUserId());
        String date1 = DateUtil.format(date, "yyyy-MM-dd");
        String date2 = DateUtil.format(date, "yyyy-MM");
        //签到
        signInfoList.setCreateTime(date);
        signInfoList.setCreateDate(date1);
        signInfoList.setCreateMonth(date2);
        signPunchMapper.insert(signInfoList);

        //插图
        Long id = signInfoList.getId();
        String images = signDTO.getImages();
        SignPicture signPicture = new SignPicture();
        signPicture.setUrl(images);
        signPicture.setSignId(id);
        signPictureMapper.insert(signPicture);
    }

    @Override
    public List<SignAllUserDTO> queryMembers(String dateStr) {
        List<SignAllUserDTO> list = new ArrayList<>();
        //校验参数
        if (dateStr == null) {
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        //查询当前员工签到情况
        LambdaQueryWrapper<SignInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SignInfo::getCompanyUserId, CurrentUserHolder.get().getCompanyUserId());
        wrapper.eq(SignInfo::getCreateDate, dateStr);
        List<SignInfo> signInfoList = signPunchMapper.selectList(wrapper);


        if (CollectionUtil.isNotEmpty(signInfoList)) {
            List<SignDTO> signDTOS = BeanHelper.copyWithCollection(signInfoList, SignDTO.class);
            for (SignDTO signDTO : signDTOS) {
                SysCompanyUserDTO data = sysUserFeign.queryByCompanyUserId(signDTO.getCompanyUserId()).getData();

                SignAllUserDTO signAllUserDTO = new SignAllUserDTO();
                signAllUserDTO.setSignResponseList(signDTOS);
                signAllUserDTO.setUsername(data.getUserName());
                list.add(signAllUserDTO);


                LambdaQueryWrapper<SignInfo> countWrapper = new LambdaQueryWrapper<>();
                countWrapper.eq(SignInfo::getCompanyId, CurrentUserHolder.get().getCompanyId());
                countWrapper.eq(SignInfo::getCompanyUserId, signDTO.getCompanyUserId());
                countWrapper.eq(SignInfo::getCreateDate, dateStr);
                Integer integer = signPunchMapper.selectCount(wrapper);


                signDTO.setDayCount(String.valueOf(integer));

                if (data != null) {
                    String userName = data.getUserName();
                    signDTO.setUserName(userName);
                }
            }

        }
        //组装数据返回

        return list;
    }




    //获取当前用户签到次数
    @Override
    public Integer getDayCount() {

        Integer selectCount = 0;
        //获取系统用户ID
        Long companyUserId = CurrentUserHolder.get().getCompanyUserId();
        Long companyId = CurrentUserHolder.get().getCompanyId();

        //根据系统用户ID查询签到信息  -------sign_info
        LambdaQueryWrapper<SignInfo> signInfoDTOWrapper = new LambdaQueryWrapper<>();

        signInfoDTOWrapper.eq(SignInfo::getCompanyUserId, companyUserId);
        signInfoDTOWrapper.eq(SignInfo::getCompanyId, companyId);

        //查到了15条签到数据
        List<SignInfo> signInfo = signPunchMapper.selectList(signInfoDTOWrapper);
        selectCount = signPunchMapper.selectCount(signInfoDTOWrapper);

        //如果没有查询到签到信息
        if (selectCount == 0) {
            throw new NcException(ResponseEnum.SIGN_DATA_NULL);
        }

        SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
        sdf.applyPattern("yyyy-MM-dd");// a为am/pm的标记
        Date date = new Date();// 获取当前时间
        String nowTime = sdf.format(date);

        //遍历签到信息列表,获取当前签到时间
        for (SignInfo info : signInfo) {
            String createDate = info.getCreateDate();
            if (createDate.equals(nowTime)) {
                return selectCount;
            } else {
                signInfoDTOWrapper.eq(SignInfo::getCreateDate, nowTime);
                selectCount = signPunchMapper.selectCount(signInfoDTOWrapper);
            }
        }
        return selectCount;
    }


    //查询本月签到记录
    @Override
    public List<SignDTO> pagingSignCurrentUserByMonth(String dateStr) {

        //校验数据
        if (StrUtil.isEmpty(dateStr)) {
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        //组装条件查询签到数据
        LambdaQueryWrapper<SignInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SignInfo::getCompanyUserId, CurrentUserHolder.get().getCompanyUserId());
        wrapper.eq(SignInfo::getCompanyId, CurrentUserHolder.get().getCompanyId());
        wrapper.eq(SignInfo::getCreateMonth, dateStr);

        List<SignInfo> signInfoList = signPunchMapper.selectList(wrapper);

        List<SignDTO> signDTOS = BeanHelper.copyWithCollection(signInfoList, SignDTO.class);

        for (SignDTO signDTO : signDTOS) {
            //根据签到ID , 查询签到图片信息
            LambdaQueryWrapper<SignPicture> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(SignPicture::getSignId, signDTO.getId());

            List<SignPicture> signPictures = signPictureMapper.selectList(wrapper1);

            if(CollectionUtil.isNotEmpty(signPictures)){
                List<String> urlList = signPictures.stream().map(signPicture -> {
                    return signPicture.getUrl();
                }).collect(Collectors.toList());

                signDTO.setSignPictureUrlList(urlList);
            }
        }

        //组装数据并返回
        return signDTOS;
    }
}
